package com.egg.ylt.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.adapter.ADA_AlbumList;
import com.egg.ylt.pojo.ActionVideoEntity;
import com.egg.ylt.pojo.DiaryPhotoEntity;
import com.egg.ylt.pojo.NatatoriumByidEntity;
import com.egg.ylt.presenter.impl.ShopAlbumPresenterImpl;
import com.egg.ylt.view.IShopAlbumView;
import com.egg.ylt.widget.EmptyControlVideo;
import com.egg.ylt.widget.FullyGridLayoutManager;
import com.egg.ylt.widget.browseimg.JBrowseImgActivity;
import com.egg.ylt.widget.browseimg.util.JMatrixUtil;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.yonyou.framework.library.adapter.rv.MultiItemTypeAdapter;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.base.BaseAppCompatActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.common.utils.StringUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by qiuzhenhuan on 2019/2/26.
 */

public class ACT_ShopAlbum extends BaseActivity<ShopAlbumPresenterImpl> implements IShopAlbumView {

    // TYPE_ALBUM 相册  TYPE_LIVE 直播
    public static final int TYPE_ALBUM = 0;
    public static final int TYPE_LIVE = 1;

    private static final String KEY_ALBUM_TYPE = "album_type";
    private static final String KEY_BANNER_LIST = "banner_list";
    private static final String KEY_SHOP_NAME = "shop_name";
    // add by dlx  添加直播地址
    private static final String KEY_VIDEO_URL = "video_url";

    @BindView(R.id.include_rl_view)
    RelativeLayout rlIncludeView;
    @BindView(R.id.include_tv_title)
    TextView tvIncludeTitle;
    @BindView(R.id.rl_shop_album_title)
    RelativeLayout rlShopAlbumTitle;
    @BindView(R.id.tv_shop_album_title)
    TextView tvShopAlbumTitle;
    @BindView(R.id.ll_album_title)
    LinearLayout llAlbumTitle;
    @BindView(R.id.ec_video)
    EmptyControlVideo emptyControlVideo;
    @BindView(R.id.rv_album)
    RecyclerView rvAlbum;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;

    private int mType;
    private String mShopName,videoUrl;
    private ArrayList<NatatoriumByidEntity.BannerListBean> bannerList;
    private ArrayList<String> bannerDisplayList;

    @Override
    protected void getBundleExtras(Bundle extras) {

        mShopName = extras.getString(KEY_SHOP_NAME);
        mType = extras.getInt(KEY_ALBUM_TYPE);
        videoUrl = extras.getString(KEY_VIDEO_URL);
        initData((List<NatatoriumByidEntity.BannerListBean>) extras.getSerializable(KEY_BANNER_LIST));
    }

    private void initData(List<NatatoriumByidEntity.BannerListBean> banners) {
        bannerList = new ArrayList<>();
        bannerDisplayList = new ArrayList<>();
        if (!ListUtil.isListEmpty(banners)) {
            for (NatatoriumByidEntity.BannerListBean bean : banners) {
                if (bean.getImageType() == NatatoriumByidEntity.IMAGE_TYPE_BANNER ||
                        bean.getImageType() == NatatoriumByidEntity.IMAGE_TYPE_ALBUM) {
                    bannerList.add(bean);
                    bannerDisplayList.add(bean.getImageUrl());
                }
            }
        }
    }


    @Override
    protected void initViewsAndEvents() {

        rlIncludeView.setVisibility(mType == TYPE_LIVE ? View.GONE : View.VISIBLE);
        rlShopAlbumTitle.setVisibility(mType == TYPE_LIVE ? View.VISIBLE : View.GONE);
        llAlbumTitle.setVisibility(mType == TYPE_LIVE ? View.VISIBLE : View.GONE);
        emptyControlVideo.setVisibility(mType == TYPE_LIVE ? View.VISIBLE : View.GONE);
        if (mType == TYPE_LIVE) {
            tvShopAlbumTitle.setText(StringUtil.getString(mShopName));
            llAlbumTitle.setVisibility(View.VISIBLE);
        } else {
            tvIncludeTitle.setText("商家相册");
        }
        rvAlbum.setFocusable(false);
        rvAlbum.setLayoutManager(new FullyGridLayoutManager(mContext, 2));
        ADA_AlbumList adapter = new ADA_AlbumList(mContext);
        rvAlbum.setAdapter(adapter);
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                List<Rect> rects = new ArrayList<>();
                if(rvAlbum.getChildCount()> 0){
                    for(int index = 0 ; index < rvAlbum.getChildCount() ; index ++){
                        ImageView ivImage = rvAlbum.getChildAt(index).findViewById(R.id.iv_image);
                        rects.add(JMatrixUtil.getDrawableBoundsInView(ivImage));
                    }
                }
                JBrowseImgActivity.start(mContext, bannerDisplayList, position, rects);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });


        if (ListUtil.isListEmpty(bannerList)) {
            rvAlbum.setVisibility(View.GONE);
            tvEmpty.setVisibility(View.VISIBLE);
        } else {
            rvAlbum.setVisibility(View.VISIBLE);
            tvEmpty.setVisibility(View.GONE);
        }
        adapter.update(bannerList, true);


        if(mType == TYPE_LIVE){
//            String source1 = "rtmp://58.200.131.2:1935/livetv/hunantv";
            emptyControlVideo.setUp(videoUrl, false, "门店直播");
            emptyControlVideo.setIsTouchWiget(false);
            emptyControlVideo.startPlayLogic();
        }
    }


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_shop_album;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }


    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }


    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @OnClick({R.id.iv_shop_album_back, R.id.include_iv_back})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.include_iv_back:
            case R.id.iv_shop_album_back:
                finish();
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (emptyControlVideo != null) {
            emptyControlVideo.onVideoPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (emptyControlVideo != null) {
            emptyControlVideo.onVideoResume();
        }
    }


    public static void startAc(Context context, int type, String shopName,String videoUrl, List<NatatoriumByidEntity.BannerListBean> bannerList) {
        Intent intent = new Intent(context, ACT_ShopAlbum.class);
        intent.putExtra(KEY_ALBUM_TYPE, type);
        intent.putExtra(KEY_SHOP_NAME, shopName);
        intent.putExtra(KEY_BANNER_LIST, (Serializable) bannerList);
        intent.putExtra(KEY_VIDEO_URL,videoUrl);
        context.startActivity(intent);
    }


}
